package com.itheima.servlet;

import com.alibaba.fastjson.JSON;
import com.itheima.bean.Brand;
import com.itheima.service.BrandService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/addBrand")
public class AddBrandServlet extends HttpServlet {
    //添加品牌
    private final BrandService brandService = new BrandService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //得到前端发来的品牌对象的信息
        String json = req.getReader().readLine();
        PrintWriter writer = resp.getWriter();
        //将品牌对象信息封装
        Brand brand = JSON.parseObject(json, Brand.class);
        //调用业务层向数据库写入品牌信息
        Boolean add = brandService.add(brand);
        if(add){
            //添加成功
            writer.print("true");
        }else {
            writer.print("false");
        }
    }
}
